package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ActivityCollect;
import com.ruoyi.system.domain.ActivityCollectUser;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.mapper.ActivityCollectUserMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 活动收集人员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-04
 */
@Service
public class ActivityCollectUserServiceImpl implements IActivityCollectUserService
{
    @Autowired
    private ActivityCollectUserMapper activityCollectUserMapper;
    @Autowired
    private IActivityCollectService activityCollectService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;

    /**
     * 查询活动收集人员
     *
     * @param id 活动收集人员主键
     * @return 活动收集人员
     */
    @Override
    public ActivityCollectUser selectActivityCollectUserById(Long id)
    {
        return activityCollectUserMapper.selectActivityCollectUserById(id);
    }

    /**
     * 查询活动收集人员列表
     *
     * @param activityCollectUser 活动收集人员
     * @return 活动收集人员
     */
    @Override
    public  List<ActivityCollectUser> selectActivityCollectUserList(ActivityCollectUser activityCollectUser)
    {
        List<ActivityCollectUser> activityCollectUsers = activityCollectUserMapper.selectActivityCollectUserList(activityCollectUser);
        for(ActivityCollectUser vo : activityCollectUsers){
            Long userId = vo.getUserId();
            SysUser user = sysUserService.selectUserById(userId);
            vo.setSysUser(user);
        }
        return activityCollectUsers;
    }

    /**
     * 新增活动收集人员
     *
     * @param activityCollectUser 活动收集人员
     * @return 结果
     */
    @Override
    public  int insertActivityCollectUser(ActivityCollectUser activityCollectUser)
    {
        activityCollectUser.setCreateTime(DateUtils.getNowDate());
        return activityCollectUserMapper.insertActivityCollectUser(activityCollectUser);
    }

    /**
     * 修改活动收集人员
     *
     * @param activityCollectUser 活动收集人员
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivityCollectUser(ActivityCollectUser activityCollectUser)
    {

        activityCollectUser.setUpoladTime(DateUtils.getNowDate());
        activityCollectUser.setUpdateTime(DateUtils.getNowDate());
        ActivityCollectUser activityCollectUserVo = activityCollectUserMapper.selectActivityCollectUser(activityCollectUser);
        Long collectId = activityCollectUserVo.getCollectId();
        ActivityCollect activityCollect = activityCollectService.selectActivityCollectById(collectId);
        Date endTime = activityCollect.getEndTime();
        Date nowDate = DateUtils.getNowDate();
        int compare = DateUtil.compare(endTime, nowDate);
        if(compare>0){
            //提交
            activityCollectUser.setStatus("1");
        }else{
            //补交
            activityCollectUser.setStatus("2");
        }
        activityCollectUser.setId(activityCollectUserVo.getId());
        int i = activityCollectUserMapper.updateActivityCollectUser(activityCollectUser);
        //添加对应归档文件
        Long activityId = activityCollect.getActivityId();
        //获取会议归纳文件夹信息
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(activityId));
        //如果是上传之后重新编辑资料，删除归纳文件重新上传
        DeptFileArchived deptFileArchivedSummary = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(activityCollectUser.getId()));
        if(!ObjectUtils.isEmpty(deptFileArchivedSummary)){
            deptFileArchivedService.deleteDeptFileArchivedByAssociationId(deptFileArchivedSummary.getAssociationId());
        }
        //创建活动收集文件夹
        DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
        deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
        String createBy = activityCollectUser.getCreateBy();
        deptFileArchivedVo.setCreateBy(createBy);
        SysUser user = sysUserService.selectUserById(Long.valueOf(createBy));
        String userName = user.getNickName();
        deptFileArchivedVo.setCreateName(userName);
        deptFileArchivedVo.setFileType("0");
        deptFileArchivedVo.setFileName("活动收集文件");
        deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
        deptFileArchivedVo.setAssociationId(String.valueOf(activityCollectUser.getId()));
        Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
        //创建活动收集文件
        DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
        deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
        deptFileArchivedVo2.setCreateBy(createBy);
        deptFileArchivedVo2.setCreateName(userName);
        deptFileArchivedVo2.setFileType("1");
        deptFileArchivedVo2.setFileName("活动收集文件附件");
        deptFileArchivedVo2.setFileUrl(activityCollectUser.getFileUrl());
        deptFileArchivedVo2.setAssociationId(String.valueOf(activityCollectUser.getId()));
        deptFileArchivedVo2.setParentId(archivedId);
        deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        return i;
    }
    /**
     * 删除活动收集人员信息
     *
     * @param id 活动收集人员主键
     * @return 结果
     */
    @Override
    public int deleteActivityCollectUserById(Long id)
    {
        return activityCollectUserMapper.deleteActivityCollectUserById(id);
    }

}
